<?php
/**
 * /nmox/modules/Sessionizer.class.php
 * Sessionizer Module
 * 
 * @author David Christian Liedle <david.liedle@gmail.com>
 * @link   http://nmox.org/
 *
 */

class Sessionizer {
    
    // handles sessions
    //var $current_session;
    
    function Sessionizer($session_name="NMOX"){
        // constructor
        session_name($session_name);
        session_start();
    }
    
    function set_var($varname, $vardata){
        $_SESSION[$varname] = $vardata;
    }
    
    function get_var($varname){
        if( $_SESSION[$varname] ){
            return $_SESSION[$varname];
        } else {
            return "BLANK";
        }
    }
    
    function cookiefy($varname, $days=365){
        // set a cookie for a session var
        $expires = time()+60*60*24*$days;
        if( $_SESSION[$varname] ){
            setcookie($varname, $_SESSION[$varname], $expires, "/");
        } else {
            setcookie($varname, "BLANK", $expires, "/");
        }
    }
    
    function cookie_munch($varname){
        // set a session var based on cookie
        if( $_COOKIE[$varname] ){
            $this->set_var($varname, $_COOKIE[$varname]);
        } else {
            $this->set_var($varname, "BLANK");
        }
    }
    
    function destroy(){
        if( isset($_COOKIE[session_name()]) ){
            // expire the session cookie
            setcookie(session_name(), '', time()-42000, "/");
        }
        session_destroy();
    }
    
}

locateMe("/nmox/modules/Sessionizer.class.php");
